/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: Crawl.java
 * @Package phoenics.apeak.crawl
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:32
 * @version V1.0
 */
package phoenics.apeak.crawl;


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

// TODO: Auto-generated Javadoc
/**
 * The Class Crawl.
 *
 * @Description The Class Crawl.
 */
public class Crawl {
	
/**
 * Inits the this.
 *
 * @param config
 *            the config
 * @param bootstrap
 *            the bootstrap
 * @param params
 *            the params
 * @return the bootstrap
 * @Description Inits the this.
 */
	private Bootstrap initThis(Class<? > config,Class<? extends Bootstrap> bootstrap,String params){
		if(!config.isAnnotationPresent(Configuration.class)){
			return null;
		}
		@SuppressWarnings("resource")
		ApplicationContext ctx = new AnnotationConfigApplicationContext(config);
		return ctx.getBean(bootstrap);
	}
	
/**
 * Run crawl.
 *
 * @param config
 *            the config
 * @param bootstrap
 *            the bootstrap
 * @Description Run crawl.
 */
	public  void runCrawl(Class<?>  config,Class<? extends Bootstrap> bootstrap){
		runCrawl(config,bootstrap,null);
	}
	
	/**
 * Run crawl.
 *
 * @param config
 *            the config
 * @param bootstrap
 *            the bootstrap
 * @param params
 *            the params
 * @Description Run crawl.
 */
	public  void runCrawl(Class<? > config,Class<? extends Bootstrap> bootstrap,String params){
		Bootstrap boot =initThis(config,bootstrap,params);
		if(boot==null)
		{
			return;
		}
		boot.start(params);
		
	}
	
	/**
 * Run crawl4 ini.
 *
 * @param config
 *            the config
 * @param bootstrap
 *            the bootstrap
 * @param params
 *            the params
 * @Description Run crawl4 ini.
 */
	public  void runCrawl4Ini(Class<? > config,Class<? extends Bootstrap> bootstrap,String params){
		Bootstrap boot =initThis(config,bootstrap,params);
		if(boot==null)
		{
			return;
		}
		boot.initializeCrawl(params);
	}
	
	/**
 * Run crawl4 ini.
 *
 * @param config
 *            the config
 * @param bootstrap
 *            the bootstrap
 * @Description Run crawl4 ini.
 */
	public  void runCrawl4Ini(Class<? > config,Class<? extends Bootstrap> bootstrap){
		runCrawl4Ini(config,bootstrap,null);
	}
}
